package com.sample.springbatch.job.sample.config;

import com.sample.springbatch.config.BaseConfig;
import com.sample.springbatch.job.sample.service.TaskletStepService;
import com.sample.springbatch.job.sample.tasklet.ContinuableTasklet;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.step.tasklet.MethodInvokingTaskletAdapter;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;


/**
 * Created by zwg on 2017/7/14.
 */
@Configuration
public class TaskletJobConfig extends BaseConfig {



    @Autowired
    public DataSource dataSource;




    @Bean(name = "taskLetJob")
    public Job taskLetJob() {
        return jobBuilderFactory.get("taskLetJob")
                .incrementer(new RunIdIncrementer())
                //.listener(listener)
                .start(taskLetStep())
                .build();
    }

    @Bean
    public Step taskLetStep() {
        return stepBuilderFactory.get("taskLetStep")
                .tasklet(continuableTasklet())
                //.tasklet(taskletShowMe())
                //.tasklet(taskletShowMe2())
                //.taskExecutor(new SyncTaskExecutor())
                //.throttleLimit(5)
                .build();
    }

    @Autowired
    TaskletStepService taskletStepService;

    @Bean
    public Tasklet taskletShowMe(){
        MethodInvokingTaskletAdapter tasklet = new MethodInvokingTaskletAdapter();
        tasklet.setTargetObject(taskletStepService);
        tasklet.setTargetMethod("showMe");
        return tasklet;
    }

    @Bean
    public Tasklet taskletShowMe2(){
        MethodInvokingTaskletAdapter tasklet = new MethodInvokingTaskletAdapter();
        tasklet.setTargetObject(taskletStepService);
        tasklet.setTargetMethod("showMe2");
        return tasklet;
    }

    @Bean
    public ContinuableTasklet continuableTasklet(){
        return new ContinuableTasklet("continuableTasklet",50);
    }
}
